package com.servlet.project.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import com.servlet.project.entity.Administrator;
import com.servlet.project.service.AdministratorService;
import com.servlet.project.service.MemberService;
import com.servlet.project.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@WebServlet(name = "AdministratorServlet",urlPatterns = "/AdministratorServlet")
public class AdministratorServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        AdministratorService administratorService=new AdministratorService();
        if(method.equals("get")){//获取当前账号
            response.setContentType("text/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            if(request.getSession().getAttribute("administrator")==null){
                writer.write(new Gson().toJson(Result.reError("请先登录")));
            }else{
                Administrator administrator=(Administrator)request.getSession().getAttribute("administrator");
                // 创建一个日期格式化对象，设置为中文
                SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
                // 创建一个Gson对象，设置日期格式化
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").setPrettyPrinting()
                        .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(chineseDateFormat.format(src)))
                        .create();
                writer.write(gson.toJson(Result.reOk("",administrator)));
            }
        }else if(method.equals("up")){//修改当前个人信息
            response.setContentType("text/json;charset=utf-8");
            String adminName=request.getParameter("adminname");
            String adminPassword=request.getParameter("adminpassword");
            String adminBirthdate=request.getParameter("adminbirthdate");
            String adminSex=request.getParameter("adminsex");
            String adminEmail=request.getParameter("adminemail");
            String adminId=request.getParameter("adminid");
            List<Object> objects=new ArrayList<>();
            objects.add(adminName);
            objects.add(adminPassword);
            objects.add(adminBirthdate);
            objects.add(adminSex);
            objects.add(adminEmail);
            objects.add(adminId);
            int count=administratorService.up(objects);
            PrintWriter writer = response.getWriter();
            if(count==1){
                writer.write(new Gson().toJson(Result.reOk("修改成功")));
            }else{
                writer.write(new Gson().toJson(Result.reError("修改失败")));
            }
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }

}
